﻿// SyntheticDataGenerator.TaxonomyCreator
//
// (c) 2011 Arthur Pitman
//
// Part of Market-Basket Synthetic Data Generator
// A C# adaptation of the IBM Quest Market-Basket Synthetic Data Generator
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// (Version 2) as published by the Free Software Foundation.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public
// License (Version 2) along with this program; if not, write to 
// the Free Software Foundation, Inc., 
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// or see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SyntheticDataGenerator.Generators;
using System.IO;
using SyntheticDataGenerator.ParameterSets;
using SyntheticDataGenerator.Core;

namespace SyntheticDataGenerator
{
	/// <summary>
	/// Creates a taxonomy
	/// </summary>
	public class TaxonomyCreator
	{	
		/// <summary>
		/// Initializes a new <see cref="TaxonomyCreator"/> instance
		/// </summary>
		/// <param name="parameters"></param>
		public TaxonomyCreator(TaxonomyParameters parameters)		
		{
			Console.WriteLine("Creating taxonomy:");
			parameters.Write(Console.Out);
			parameters.WriteToFile(parameters.BaseFilename + ".config");

			// Set master seed for random seed generator
			if (parameters.MasterSeed < 0)
				SeedGenerator.SetMasterSeed(parameters.MasterSeed);

			// generate taxonomy and write it to file
			var taxonomy = new Taxonomy(parameters.ItemCount, parameters.RootCount, parameters.Fanout, parameters.DepthRatio);
			var taxonomyWriter = File.CreateText(parameters.BaseFilename + ".taxonomy");
			taxonomy.Write(taxonomyWriter);
			taxonomyWriter.Close();

			var transactionLengthGenerator = new PoissonGenerator(parameters.AverageTransactionLength - 1);
			var litsPatternSet = new TaxonomyPatternSet(parameters.ItemCount, parameters.LisParameters, taxonomy);

			// write patterns to a file
			var patternWriter = File.CreateText(parameters.BaseFilename + ".patterns");			
			litsPatternSet.Write(patternWriter);
			patternWriter.Close();

			// write transactions to a file
			var transactionWriter = File.CreateText(parameters.BaseFilename + ".transactions");
			var patternSetIterator = new PatternSetIterator(litsPatternSet);
			for (int i = 0; i < parameters.TransactionCount; i++)
			{
				var trans = new Transaction(patternSetIterator, transactionLengthGenerator.NextInt() + 1);
				trans.Write(transactionWriter);
			}
			transactionWriter.Close();
		}
	}
}